home *** CD-ROM | disk | FTP | other *** search
/ Winzipper / Winzipper_ISO.iso / programming / oracle7 7.2 / DB / UTIL72 / EXAMP10.PC < prev    next >
Encoding:
Text File  |  1995-05-18  |  4.5 KB  |  132 lines

  1. #ifdef RCSID
  2. static char *RCSid = 
  3.    "$Header: examp10.pc.d,v 1.2.720.1 95/02/21 17:50:05 xxxxxxxx: Needtomrg_7_2 $ ";
  4. #endif /* RCSID */
  5.  
  6. /* Copyright (c) 1991 by Oracle Corporation */
  7. /*
  8.    NAME
  9.      examp10.pc - <one-line expansion of the name>
  10.    DESCRIPTION
  11.      <short description of component this file declares/defines>
  12.    PUBLIC FUNCTION(S)
  13.      <list of external functions declared/defined - with one-line descriptions>
  14.    PRIVATE FUNCTION(S)
  15.      <list of static functions defined in .c file - with one-line descriptions>
  16.    RETURNS
  17.      <function return values, for .c file with single function>
  18.    NOTES
  19.      <other useful comments, qualifications, etc.>
  20.    MODIFIED   (MM/DD/YY)
  21.     xxxxxxxx   02/13/95 -  update
  22.     xxxxxxxx   05/12/92 -  Creation 
  23. */
  24. /************************************************************************
  25.  *                                              *
  26.  *  EMBEDDED PL/SQL DEMO                                                *
  27.  *                                                                      *
  28.  *  This program prompts for an account number and a debit or credit    *
  29.  *  amount.  If the account number is valid, a credit always succeeds,  *
  30.  *  but a debit succeeds only if there are sufficient funds.            *
  31.  *                                    *
  32.  *  Copyright (c) 1989,1992 by Oracle Corporation                       *
  33.  ************************************************************************/
  34.  
  35. #include <stdio.h>
  36.  
  37. EXEC SQL BEGIN DECLARE SECTION;
  38.    int     acct, amount;
  39.    VARCHAR tran_type[10];
  40.    VARCHAR status[65];
  41.    VARCHAR uid[20];
  42.    VARCHAR pwd[20];
  43. EXEC SQL END DECLARE SECTION;
  44.  
  45. EXEC SQL INCLUDE SQLCA;
  46.  
  47. void sqlerror();
  48. main()
  49. {
  50.       /* Set up userid and password */
  51.    strcpy (uid.arr,"scott");
  52.    uid.len=strlen(uid.arr);
  53.    strcpy (pwd.arr,"tiger");
  54.    pwd.len=strlen(pwd.arr);
  55.  
  56.    printf("\n\n\tEmbedded PL/SQL Demo\n\n");
  57.    printf("Trying to connect...");
  58.       /* Check for SQL errors */
  59.    EXEC SQL WHENEVER SQLERROR DO sqlerror();
  60.       /* Connect to Oracle */
  61.    EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
  62.    printf(" connected.\n");
  63.  
  64.    for (;;) /* Loop indefinitely */
  65.    {
  66.       printf("\n\n** Account number? (-1 to quit)");
  67.       scanf("%d", &acct);
  68.       if (acct == -1)   /* Disconnect from Oracle and */
  69.       {                 /* exit program if acct is -1 */
  70.          EXEC SQL COMMIT WORK RELEASE;
  71.          exit(0);
  72.       }
  73.       printf("\n   Transaction type? (C)redit or (D)ebit   ");
  74.       scanf("%s", &tran_type.arr);
  75.       tran_type.len = 1;   /* Only want first character */
  76.       printf("\n   Transaction amount? (in whole dollars)  ");
  77.       scanf("%d", &amount);
  78.  
  79.       /* ----- Begin PL/SQL block ----- */
  80.       EXEC SQL EXECUTE
  81.       DECLARE
  82.          old_bal    NUMBER(11,2);
  83.          no_account EXCEPTION;
  84.       BEGIN
  85.          :tran_type := UPPER(:tran_type);
  86.          IF :tran_type = 'C' THEN       -- credit the account
  87.             UPDATE accounts SET bal = bal + :amount
  88.                WHERE account_id = :acct;
  89.             IF SQL%ROWCOUNT = 0 THEN    -- no rows affected
  90.                RAISE no_account;
  91.             ELSE
  92.                :status := 'Credit complete.';
  93.             END IF;
  94.          ELSIF :tran_type = 'D' THEN    -- debit the account
  95.             SELECT bal INTO old_bal FROM accounts
  96.                WHERE account_id = :acct;
  97.             IF old_bal >= :amount THEN  -- has sufficient funds
  98.                 UPDATE accounts SET bal = bal - :amount
  99.                    WHERE account_id = :acct;
  100.                :status := 'Debit applied';
  101.             ELSE
  102.                :status := 'Insufficient funds';
  103.             END IF;
  104.          ELSE
  105.             :status := :tran_type || ' is an illegal transaction';
  106.          END IF;
  107.          COMMIT;
  108.       EXCEPTION
  109.          WHEN NO_DATA_FOUND OR no_account THEN
  110.             :status := 'Nonexistent account';
  111.          WHEN OTHERS THEN
  112.             :status := 'Error: ' || SQLERRM(SQLCODE);
  113.       END;
  114.       END-EXEC;
  115.       /* ----- End the PL/SQL block ----- */
  116.   
  117.       status.arr[status.len] = '\0';  /* null-terminate string */
  118.       printf("\n\n   Status: %s", status.arr);
  119.    }  /* End of loop */
  120. }
  121.  
  122. void sqlerror()
  123. {
  124.       /* Avoid infinite loop if rollback causes an error */ 
  125.    EXEC SQL WHENEVER SQLERROR CONTINUE;
  126.    printf("\nOracle error detected:\n");
  127.       /* Print error message and disconnect from Oracle */
  128.    printf("\n%.70s\n", sqlca.sqlerrm.sqlerrmc);
  129.    EXEC SQL ROLLBACK WORK RELEASE;
  130.    exit(1);
  131. }
  132.